<?php

namespace app\admin\controller\setup;

use app\common\model\SystemGoods;
use app\common\model\SystemGoodsDetail;
use library\Controller;
use think\Db;

/**
 * 选项管理
 * Class Options
 * @package app\admin\controller\setup
 */
class Options extends Controller
{
    /**
     * 指定数据表
     * @var string
     */
    protected $table = 'SystemDropdown';

    /**
     * 选项列表
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '选项管理';
        $where = [];
		$where[] = ['is_deleted','eq',0];
        $query = $this->_query($this->table)->like('name');
        $query->where($where)->order('id asc')->page();
    }


    /**
     * 禁用下拉项
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        $this->applyCsrfToken();
        $parameter = $this->request->post();
        $options = Db::name('system_options')->where(['id'=>$parameter['id']])->find();
        $dropdown = Db::name($this->table)->where(['id' => $options['dropdown_id']])->find();
        
        Db::name('system_options')->where(['id'=>$parameter['id']])->update(['status'=>2]);
        
        //删除订单选项缓存
        if($dropdown['type'] == 1){
        	$order_all_slug = 'order_all_'.$dropdown['slug'];
            $order_normal_slug = 'order_normal_'.$dropdown['slug'];
            cache($order_all_slug, NULL);
            cache($order_normal_slug, NULL);
        } 
        
        $this->success('数据更新成功!');
    }
    
    

    /**
     * 启用下拉项
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        $this->applyCsrfToken();
        $parameter = $this->request->post();
        $options = Db::name('system_options')->where(['id'=>$parameter['id']])->find();
        $dropdown = Db::name($this->table)->where(['id' => $options['dropdown_id']])->find();
        
        Db::name('system_options')->where(['id'=>$parameter['id']])->update(['status'=>1]);
        
        //删除订单选项缓存
        if($dropdown['type'] == 1){
        	$order_all_slug = 'order_all_'.$dropdown['slug'];
            $order_normal_slug = 'order_normal_'.$dropdown['slug'];
            cache($order_all_slug, NULL);
            cache($order_normal_slug, NULL);
        } 
        
        $this->success('数据更新成功!');
    }
    
    
    

    /**
     * 下拉项列表
     * @auth true
     *
     */
    public function optionList()
    {
        if ($this->request->isGet()) {
            $parameter = $this->request->get();
            $dropdown = Db::name($this->table)->where(['id' => $parameter['id']])->find();
            //删除订单选项缓存
	        if($dropdown['type'] == 1){
	        	$order_all_slug = 'order_all_'.$dropdown['slug'];
	            $order_normal_slug = 'order_normal_'.$dropdown['slug'];
	            cache($order_all_slug, NULL);
	            cache($order_normal_slug, NULL);
	        }
            $this->dropdown_id = $parameter['id'];
            $this->title = '选项管理  /  ' . $dropdown['name'];

            $this->list = $this->_query('system_options')->where(['dropdown_id' => $parameter['id'],'is_deleted'=>0])->order('sort desc,id asc')->page();
                
            $this->fetch('option_list');
        }else{
        	$parameter = $this->request->post();
        	$options = Db::name('system_options')->where(['id'=>$parameter['id']])->find();
        	Db::name('system_options')->where(['id'=>$parameter['id']])->update(['sort'=>$parameter['sort']]);
        	$dropdown = Db::name($this->table)->where(['id' => $options['dropdown_id']])->find();
            $this->dropdown_id = $parameter['id'];
            $this->title = '选项管理  /  ' . $dropdown['name'];

            $this->list = $this->_query('system_options')->where(['dropdown_id' => $options['dropdown_id']])->order('sort desc,id asc')->page();
                            
            $this->fetch('option_list');
        }
    }

    /**
     * 添加下拉项
     * @auth true
     *
     */
    public function addOption()
    {
        if ($this->request->isGet()) {
            $parameter = $this->request->get();
            $this->dropdown_id = $parameter['dropdown_id'];
            
            $this->fetch('option_form');
        } else {
            $parameter = $this->request->post();
            
            $where = [['name', 'eq', $parameter['name']],['dropdown_id','eq',$parameter['dropdown_id']]];
            
            if (!empty($parameter['id'])) $where[] = ['id ', 'neq', $parameter['id']];
            if (Db::name('system_options')->where($where)->count() > 0) {
                $this->error('选项名称已存在，请更改选项名称');
            }
            $dropdown = Db::name($this->table)->where(['id' => $parameter['dropdown_id']])->find();
            if(!$dropdown){
            	$this->error('下拉项ID错误');
            }
            //查找下拉项最后一个选项ID
            $last =  Db::name('system_options')->where(['dropdown_id' => $parameter['dropdown_id']])->order('option_id desc')->find();
            if($last){
            	$option_id = $last['option_id'] + 1;
            }else{
            	$option_id = 1;
            }
            Db::startTrans();
            try {           	
				$data = [];
	            $data['name'] = $parameter['name'];
	            $data['slug'] = $dropdown['slug'];
	            $data['option_id'] = $option_id;
	            $data['sort'] = $parameter['sort'];
	            $data['desc'] = $parameter['desc'];
	            $data['dropdown_id'] = $parameter['dropdown_id'];
	            $data['dropdown_name'] = $dropdown['name'];
	            Db::name('system_options')->insert($data);
	            
	            //更新时间
	            $update_at = date('Y-m-d H:i:s',time());
	            Db::name($this->table)->where(['id' => $parameter['dropdown_id']])->update(['update_at'=>$update_at]);
	            
	            //删除订单选项缓存
	            if($dropdown['type'] == 1){
	            	$order_all_slug = 'order_all_'.$dropdown['slug'];
		            $order_normal_slug = 'order_normal_'.$dropdown['slug'];
		            cache($order_all_slug, NULL);
		            cache($order_normal_slug, NULL);
	            }            
	            
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error('保存失败，请重试');
            }
            
            $this->success('保存成功');
        }
    }
    
    
    /**
     * 编辑下拉项
     * @auth true
     *
     */
    public function editOption()
    {
        if ($this->request->isGet()) {
            $parameter = $this->request->get();
            $options = Db::name('system_options')->where(['id' => $parameter['id']])->find();
            if(!$options){
            	$this->error('下拉项ID错误');
            } 
            $this->dropdown_id = $options['dropdown_id'];
            $this->info = $options;
            $this->fetch('option_form');
        } else {
            $parameter = $this->request->post();
            
            $where = [['name', 'eq', $parameter['name']],['dropdown_id','eq',$parameter['dropdown_id']]];
            
            if (!empty($parameter['id'])) $where[] = ['id ', 'neq', $parameter['id']];
            if (Db::name('system_options')->where($where)->count() > 0) {
                $this->error('选项名称已存在，请更改选项名称');
            }
            $dropdown = Db::name($this->table)->where(['id' => $parameter['dropdown_id']])->find();
            if(!$dropdown){
            	$this->error('下拉项ID错误');
            }    
            Db::startTrans();
            try {           	
				$data = [];
	            $data['name'] = $parameter['name'];	            
	            $data['desc'] = $parameter['desc'];	
	            $data['sort'] = $parameter['sort'];            
	            Db::name('system_options')->where(['id'=>$parameter['id']])->update($data);
	            
	            //更新时间
	            $update_at = date('Y-m-d H:i:s',time());
	            Db::name($this->table)->where(['id' => $parameter['dropdown_id']])->update(['update_at'=>$update_at]);
	            
	            //删除订单选项缓存
	            if($dropdown['type'] == 1){
	            	$order_all_slug = 'order_all_'.$dropdown['slug'];
		            $order_normal_slug = 'order_normal_'.$dropdown['slug'];
		            cache($order_all_slug, NULL);
		            cache($order_normal_slug, NULL);
	            }
	            
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->error('保存失败，请重试');
            }
            
            $this->success('保存成功');
        }
    }
   
}
